<script>
import { reactive, toRefs, getCurrentInstance } from "vue";
import { useRouter } from "vue-router";
import LoginShow from "./components/LoginShow.vue";
export default {
  components: {
    LoginShow,
  },
  setup() {
    const state = reactive({
      form: {
        account: "",
        password: "",
      },
      islogin: false,
    });
    const router = useRouter();
    const { proxy } = getCurrentInstance();

    const login = async () => {
      state.islogin = true;
      const user = await proxy.$API.auth.token.post(state.form);
      if (user.code === 200) {
        // 保存用户信息
        proxy.$TOOL.data.set("TOKEN", user.data.token);
        proxy.$TOOL.data.set("USER_INFO", user.data.userInfo);
      } else {
        state.islogin = false;
        ElMessage.warning(user.desc);
        return;
      }
      router.replace({ path: "/" });
      ElMessage.success("登录成功");
    };

    return { ...toRefs(state), login };
  },
};
</script>

<template>
  <div class="container">
    <div class="canvas-wrapper"><!-- <LoginShow /> -->hhhh</div>
    <div class="form-wrapppr">
      <div class="form">
        <el-form :model="form">
          <el-form-item>
            <h1>{{ $CONFIG.APP_NAME }}</h1>
          </el-form-item>
          <el-form-item>
            <el-input placeholder="账户" v-model="form.account">
              <template #prefix>
                <el-icon><el-icon-user /></el-icon>
              </template>
            </el-input>
          </el-form-item>
          <el-form-item>
            <el-input
              type="password"
              placeholder="请输入密码"
              v-model="form.password"
            >
              <template #prefix>
                <el-icon><el-icon-lock /></el-icon>
              </template>
            </el-input>
          </el-form-item>
        </el-form>
        <el-form-item>
          <el-button
            type="primary"
            round
            style="width: 100%"
            :loading="islogin"
            @click="login"
            >登录</el-button
          >
        </el-form-item>
      </div>
    </div>
  </div>
</template>

<style scoped>
.container {
  height: 100%;
  width: 100%;
  display: flex;
  align-items: center;
}

.container .canvas-wrapper {
  flex: 2;
  height: 100%;
  overflow: hidden;
  background-color: aqua;
}

.container .form-wrapppr {
  flex: 1;
  display: flex;
  justify-content: center;
}

.container .form-wrapppr .form {
  width: 400px;
  padding: 20px;
}
</style> 